/*
 * MODULE  NAME  :
 * PROGRAM NAME  : proto.h
 * AUTHOR        : hotmocha(hotmocha@126.com)
 * CREATE  DATE  : 2015-10-30 17:27:45
 * PROGRAM DESC  :
 *
 * HISTORY       :
 *
 */
#ifndef _H_PROTO_H_
#define _H_PROTO_H_

#include "commproto.h"
#include "constants.h"

typedef struct 
{
        int clientid;
        char clientname[41];
        char ip[41];
} member;

int add_member(memberlist *mlist, member *m);
int query_member_byname(memberlist *mlist, member *m);
int query_member_byid(memberlist *mlist, member *m);
int query_member_byip(memberlist *mlist, member *m);
int del_member(memberlist *mlist, int clientid);
void init_member_list(memberlist *mlist, int cmd_type) ;

#define MSG_TYPE_MEMBER_LIST    0
#define MSG_TYPE_CLIENT_INFO    1
#define MSG_TYPE_COMM_MSG       2
#define MSG_TYPE_LOGOUT         3
#define MSG_TYPE_STATUS         4
#define MSG_TYPE_FILE		5

/**
_WINDLL_FUNC int DSCSERIALIZE_JSON_clientinfo( clientinfo *pst , char *encoding , char *buf , int *p_len );
_WINDLL_FUNC int DSCDESERIALIZE_JSON_clientinfo( char *encoding , char *buf , int *p_len , clientinfo *pst );
**/
// SERIAL_TO_TK_MESSAGE(&member_list(*), &tmsg(*), memberlist, MSG_TYPE_MEMBER_LIST, r);
#define SERIAL_TO_TK_MESSAGE(ptr, tmsg, type_name, msg_type, r) \
        do { \
                (tmsg)->body_len = MAXMESSAGELEN; \
                r = DSCSERIALIZE_JSON_##type_name((type_name*)(ptr), "GBK", (tmsg)->body, &(tmsg)->body_len); \
                if (r) { \
                        break; \
                } \
                (tmsg)->body_left_len = (tmsg)->body_len; \
		(tmsg)->header_left_len = HEADERLEN; \
		(tmsg)->header[HEADERLEN] = '\0'; \
		sprintf((tmsg)->header, "%04d%04d", msg_type, (tmsg)->body_len); \
		(tmsg)->message_type = msg_type; \
		(tmsg)->body[(tmsg)->body_len] = '\0'; \
        } while (0)

// DESERIAL_FROM_TK_MESSAGE(&client(*), &tmsg(*), clientinfo, r);
#define DESERIAL_FROM_TK_MESSAGE(ptr, tmsg, type_name, r) \
        do { \
		DSCINIT_##type_name((type_name*)ptr);		\
                int __len__ = (tmsg)->body_len; \
                r = DSCDESERIALIZE_JSON_##type_name("GBK", (tmsg)->body, &__len__, (type_name*)(ptr)); \
                if (r) { \
                        break; \
                } \
        } while (0)


#endif
